package com.xxxx.yoyo.dao.impl;

import java.util.List;
import java.util.Map;

import com.xxxx.yoyo.bean.Goods;
import com.xxxx.yoyo.dao.IGoodsDao;
import com.xxxx.yoyo.util.DBUtils;

public class GoodsDaoImpl implements IGoodsDao{
	
	/**
	 * 根据榜单type类型查询宠物列表
	 * @param type 榜单类型 1表示今日推荐 2表示热销推荐 3表示新品推荐
	 * @return 返回宠物列表
	 */
	@Override
	public List<Goods> selectGoodsListByHotType(int type) {
		String sql = "SELECT g.* FROM top AS t JOIN goods AS g ON t.good_id = g.id WHERE type = ? AND status = 1";
		List<Goods> goodsList = DBUtils.query(sql, Goods.class, type);
		return goodsList;
	}

	/**
	 * 根据宠物分类的id查询宠物列表
	 * @param typeId 宠物分类的id
	 * @return 返回查到的宠物列表
	 */
	@Override
	public List<Goods> selectGoodsListByTypeId(int typeId) {
		String sql = "SELECT * FROM goods WHERE type_id = ? AND status = 1";
		List<Goods> goodsList = DBUtils.query(sql, Goods.class, typeId);
		return goodsList;
	}

	/**
	 * 根据宠物分类的id,页码以及每页条数查询宠物列表
	 * @param typeId 宠物分类的id
	 * @param page 页码
	 * @param size 每页条数
	 * @return 返回查到的宠物列表
	 */
	@Override
	public List<Goods> selectGoodsListByTypeId(int typeId, int page, int size) {
		String sql = "SELECT * FROM goods WHERE type_id = ? AND status = 1 limit ?, ?";
		List<Goods> goodsList = DBUtils.query(sql, Goods.class, typeId, (page-1)*size, size);
		return goodsList;
	}

	/**
	 * 根据宠物类型的id,查询宠物的个数
	 * @param typeId 宠物类型的id
	 * @return 返回对应分类下宠物的条数
	 */
	@Override
	public long selectGoodsCountByTypeId(int typeId) {
		String sql = "SELECT count(*) as count FROM goods WHERE type_id = ? AND status = 1";
		Map<String, Object> result = DBUtils.queryOne(sql, typeId);
		long count = (Long)result.get("count");
		return count;
	}

	/**
	 * 根据榜单type,page,size类型查询宠物列表
	 * @param type 榜单类型 1表示今日推荐 2表示热销推荐 3表示新品推荐
	 * @param page 当前页码
	 * @param size 每页条数
	 * @return 返回宠物列表
	 */
	@Override
	public List<Goods> selectGoodsListByHotType(int type, int page, int size) {
		String sql = "SELECT g.* FROM top AS t JOIN goods AS g ON t.good_id = g.id WHERE type = ? AND status = 1 limit ?, ?";
		List<Goods> goodsList = DBUtils.query(sql, Goods.class, type, (page-1)*size, size);
		return goodsList;
	}

	/**
	 * 根据榜单类型,查询宠物的个数
	 * @param type 榜单的类型
	 * @return 返回对应榜单下宠物的条数
	 */
	@Override
	public long selectGoodsCountByHotType(int type) {
		String sql = "SELECT count(*) AS count FROM top AS t JOIN goods AS g ON t.good_id = g.id where type = ? AND g.status = 1";
		Map<String, Object> map = DBUtils.queryOne(sql, type);
		long count = (Long)map.get("count");
		return count;
	}

	/**
	 * 按关键字keyword,page,size查询宠物列表
	 * @param keyword 宠物关键字
	 * @param page 页码
	 * @param size 每页条数
	 * @return 返回查询到的结果
	 */
	@Override
	public List<Goods> selectGoodsListByKeyword(String keyword, int page, int size) {
		String sql = "SELECT * FROM goods WHERE name LIKE '%" + keyword + "%' AND status = 1 limit ?,?";
		List<Goods> goodsList = DBUtils.query(sql, Goods.class, (page-1)*size, size);
		return goodsList;
	}

	/**
	 * 查询包含指定关键字的宠物的个数
	 * @param keyword 宠物关键字
	 * @return 返回包含指定关键字的宠物的数量
	 */
	@Override
	public long selectGoodsCountByKeyword(String keyword) {
		String sql = "SELECT count(*) AS count FROM goods WHERE name LIKE '%" + keyword + "%' AND status = 1 ";
		Map<String, Object> map = DBUtils.queryOne(sql);
		long count = (Long)map.get("count");
		return count;
	}

	/**
	 * 根据宠物的id查询宠物
	 * @param id 宠物的id
	 * @return 返回查询到的宠物
	 */
	@Override
	public Goods selectGoodsById(int id) {
		String sql = "SELECT * FROM goods WHERE id = ?";
		Goods goods = DBUtils.queryOne(sql, Goods.class, id);
		return goods;
	}

	/**
	 * 根据分页信息获取宠物列表
	 * @param page 页码
	 * @param size 每页条数
	 * @return 返回查到的宠物列表
	 */
	@Override
	public List<Goods> selectGoodsList(int page, int size) {
		String sql = "SELECT * FROM goods WHERE status = 1 limit ? , ?";
		List<Goods> goodsList = DBUtils.query(sql, Goods.class, (page-1)*size, size);
		return goodsList;
	}

	/**
	 * 获取宠物总数量
	 * @return 返回宠物总数量
	 */
	@Override
	public long selectGoodsCount() {
		String sql = "SELECT count(*) AS count FROM goods WHERE status = 1";
		Map<String, Object> map = DBUtils.queryOne(sql);
		long count = (Long)map.get ("count");
		return count;
	}

	/**
	 * 添加宠物
	 * @param goods 宠物
	 * @return 添加成功返回1，失败返回0
	 */
	@Override
	public int insertGoods(Goods goods) {
		String sql = "INSERT INTO goods (name, price, intro, cover, image1, image2, stock, type_id, status) VALUES(?,?,?,?,?,?,?,?,?)";
		int row = DBUtils.update(sql, goods.getName(), goods.getPrice(), goods.getIntro(), goods.getCover(), goods.getImage1(), goods.getImage2(), goods.getStock(), goods.getTypeId(), goods.getStatus());
		return row;
	}

	/**
	 * 根据宠物的id删除宠物---只修改数据库状态值，不真正删除
	 * @param goodsId 宠物id
	 * @return 删除成功返回1，失败返回0
	 */
	@Override
	public int deleteGoodsById(int goodsId) {
		String sql = "UPDATE goods SET status = 0 WHERE id = ?";
		int row = DBUtils.update(sql, goodsId);
		return row;
	}

	/**
	 * 更新宠物信息
	 * @param goods 宠物信息
	 * @return 成功返回1，失败返回0
	 */
	@Override
	public int updateGoods(Goods goods) {
		String sql = "UPDATE goods SET name = ?, price = ?, intro = ?, stock = ?, cover = ?, image1 = ?, image2 = ?, type_id = ? WHERE id = ?";
		int row = DBUtils.update(sql, goods.getName(), goods.getPrice(), goods.getIntro(), goods.getStock(), goods.getCover(), goods.getImage1(), goods.getImage2(), goods.getTypeId(), goods.getId());
		return row;
	}

}
